﻿using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    /// <summary>
    /// 角色操作
    /// </summary>
    public class RoleOptController : ApiController
    {
        /// <summary>
        /// 获取角色列表 eg. :0000000001|WEB 
        /// </summary>
        /// <param name="key">memberid|platcd|comcd</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/roleopt")]
        public RespSysRoleInfo GetRoleInfo(string key)
        {
            RespSysRoleInfo resp = new RespSysRoleInfo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 3)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strPlatcd = strsKeyOriginal[1];
            string strComcd = strsKeyOriginal[2];

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            if (!basecodeLogic.CheckUserPermission(strMemberid, GetType().FullName)) //****后台权限判定
            {
                resp.statusCode = 90015;
                resp.statusMsg = " 没有相关的操作权限";
                return resp;
            }

            resp = basecodeLogic.GetRoleInfo(strComcd);
            return resp;
        }

        /// <summary>
        /// 更新角色基本信息
        /// </summary>
        /// <param name="_reqInfo">更新角色请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/roleopt")]
        public RespSysNullResultInfo UpdateRoleGroupInfo(ReqRoleGroupManagement _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();

            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            if (!basecodeLogic.CheckUserPermission(_reqInfo.memberid, GetType().FullName))
            {
                resp.statusCode = 90015;
                resp.statusMsg = " 没有相关的操作权限";
                return resp;
            }

            bool isRoleGroupUpd = basecodeLogic.UpdateRoleGroupInfo(_reqInfo.memberid, _reqInfo.rolegroupinfo ?? default(RoleGroupforCUD), _reqInfo.roleinfo ?? default(RoleforCUD));
            if (!isRoleGroupUpd)
            {
                resp.statusCode = 60008;
                resp.statusMsg = "角色信息更新失败";
                return resp;
            }

            resp.statusCode = 0;
            resp.statusMsg = "更新成功";
            return resp;
        }

    }
}
